package com.ksd.pug.cloud.api2;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ksd.pug.cloud.mapper.UserMapper;
import com.ksd.pug.cloud.pojo.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2022/2/28$ 22:05$
 */
@RestController
@Slf4j
public class UserServiceFeignController3 extends ServiceImpl<UserMapper, SysUser> implements IUserServiceFeign2 {

    @Override
    public SysUser findUsers(Long id) {
        int num = ThreadLocalRandom.current().nextInt(4);
        System.out.println("user-service ----findUsers , " + num);
        try {
            Thread.sleep(num * 1000);
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return this.getById(id);
    }

    @Override
    public List<SysUser> listUser() {
        return this.list();
    }

    @Override
    public List<SysUser> listUserPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        //1 :设置分页
        Page<SysUser> page = new Page<>(pageNo, pageSize);
        //2: 设置条件
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 过滤不需要的字段
        lambdaQueryWrapper.select(SysUser.class, c -> !c.getColumn().equalsIgnoreCase("password"));
        lambdaQueryWrapper.eq(SysUser::getStatus, 1);
        //3: 设置排序
        lambdaQueryWrapper.orderByDesc(SysUser::getCreateTime);
        //4: 返回
        Page<SysUser> page1 = this.page(page, lambdaQueryWrapper);
        List<SysUser> records = page1.getRecords();
        return CollectionUtils.isEmpty(records) ? null : records;
    }

    @Override
    public Page<SysUser> pageUser( Integer pageNo, Integer pageSize) {
        //1 :设置分页
        Page<SysUser> page = new Page<>(pageNo, pageSize);
        //2: 设置条件
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 过滤不需要的字段
        lambdaQueryWrapper.select(SysUser.class, c -> !c.getColumn().equalsIgnoreCase("password"));
        lambdaQueryWrapper.eq(SysUser::getStatus, 1);
        //3: 设置排序
        lambdaQueryWrapper.orderByDesc(SysUser::getCreateTime);
        //4: 返回
        return this.page(page, lambdaQueryWrapper);
    }


    @Override
    public int saveOrUpdateUser(SysUser sysUser) {
        if (sysUser == null) {
            return 0;
        }
        return this.saveOrUpdate(sysUser) ? 1 : 0;
    }

    @Override
    public int delUserById(Long id) {
        if (id == null) {
            return 0;
        }
        return this.removeById(id) ? 1 : 0;
    }
}
